/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package etf.pia.converters;

import etf.pia.entity.Predmet;
import etf.pia.entity.Student;
import etf.pia.entity.Zaposleni;
import static etf.pia.util.MyLogger.log;
import java.util.ArrayList;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

/**
 *
 * @author Alejandro
 */
@FacesConverter(value = "studentConverter")
public class StudentConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
        log.info("get as object");
        log.info(arg2);
        log.info(arg1.getChildCount());
        ArrayList<Student> pList = ((ArrayList<Student>) ((UISelectItems) arg1.getChildren().get(0)).getValue());
        log.info(pList);
        String s = "";
        for (Student p : pList) {
            s = p.getIme() + " " + p.getPrezime();
            if (s.equals(arg2)) {
                return p;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
        log.info("get as string " + arg2);
        log.info(arg1);
        String str = "";
        if (arg2 instanceof Student) {
            str = "" + ((Student) arg2).getIme() + " " + ((Student) arg2).getIme();
        }
        return str;
    }
}
